#include "xmppmuc.h"

XmppMuc::XmppMuc(QObject *parent) : QObject(parent)
{
    m_mucManager = 0;
    m_jidRoom = "";
    m_mucRoom = 0;
    m_subjectRoom = "";
}


void XmppMuc::setMucManager(QXmppMucManager* arg)
{
    qDebug() << Q_FUNC_INFO;
    if ( (m_mucManager != arg) && (arg != 0) )
    {
        m_mucManager = arg;

        QObject::connect( m_mucManager, SIGNAL(invitationReceived(QString,QString,QString)), this, SIGNAL(invitationMucReceived(QString,QString,QString)) );

        emit mucManagerChanged();
    }
}



void XmppMuc::setJidRoom( QString jidRoom )
{
    //qDebug() << Q_FUNC_INFO << "  jidRoom:"<<jidRoom <<" m_jidRoom:"<<m_jidRoom<<"  m_mucManager:"<<m_mucManager;
    if ( (m_jidRoom != jidRoom) && (m_mucManager != 0) )
    {
        m_jidRoom = jidRoom;

        bool flNewRoom = true;
        QList< QXmppMucRoom * > rooms = m_mucManager->rooms();
        for( int L=0; L<rooms.length(); L++ )
        {
            QXmppMucRoom *mucRoom = rooms.at(L);
            if( mucRoom->jid() == jidRoom )
            {
                m_mucRoom = rooms.at(L);
                flNewRoom = false;
                break;
            }
            flNewRoom = true;
        }
        if( flNewRoom ) {
            m_mucRoom = m_mucManager->addRoom( jidRoom );
            //qDebug() << "@m_mucRoom:" << m_mucManager->rooms().length();
        }

        if( m_mucRoom ) {
            QObject::connect( m_mucRoom, SIGNAL( participantChanged(QString) ), this, SLOT( participantChangedAction(QString) ), Qt::UniqueConnection );
            QObject::connect( m_mucRoom, SIGNAL( participantAdded(QString) ), this, SLOT( participantAddedAction(QString) ), Qt::UniqueConnection );
            QObject::connect( m_mucRoom, SIGNAL( participantRemoved(QString) ), this, SLOT( participantRemovedAction(QString) ), Qt::UniqueConnection );
            QObject::connect( m_mucRoom, SIGNAL( messageReceived(QXmppMessage) ), this, SLOT( messageReceivedAction(QXmppMessage) ), Qt::UniqueConnection );
            QObject::connect( m_mucRoom, SIGNAL( subjectChanged(QString) ), this, SLOT(subjectRoomChangedAction(QString)), Qt::UniqueConnection );
        }

        emit jidRoomChanged(jidRoom);
    }

    if( m_mucRoom ) {
        if( m_mucRoom->isJoined() == false )
        {
            m_mucRoom->setNickName( m_myNickName );
            if( !(m_myPasswd.isEmpty()) ) {
                m_mucRoom->setPassword( m_myPasswd );
            }
            bool b = m_mucRoom->join();
            if( b ) {
                this->subjectRoomChangedAction( m_mucRoom->subject() );
            }
        }
    }

    //this->getParticipants();
}

QString XmppMuc::resourceAsNickName(const QString &jid) const
{
    QString nickName = "";
    int pos = jid.indexOf("/");
    if( pos >= 0 ) {
        nickName = jid.mid(pos+1);
    }
    return nickName;
}


bool XmppMuc::sendMessage( const QString text )
{
    bool r = false;
    qDebug() << Q_FUNC_INFO;
    if( m_mucRoom ) {
        r = m_mucRoom->sendMessage( text );
    }
    return r;
}

QString XmppMuc::getNameByJid( const QString mucJid ) const
{
    QString replaceStr = m_jidRoom + "/";
    QString name = mucJid;
    name.replace( replaceStr, QString("") );
    return name;
}

void XmppMuc::leaveChat()
{
    if( m_mucRoom )
    {
        if( m_mucRoom->isJoined() )
        {
            /*bool b = */m_mucRoom->leave();
            /*if( b ) {
                this->subjectRoomChangedAction( m_mucRoom->subject() );
            }*/
        }
    }
}


QStringList XmppMuc::getParticipants()
{
    qDebug() << Q_FUNC_INFO << " # m_mucRoom:" << m_mucRoom;
    QStringList retList;
    if( m_mucRoom ) {
        QStringList participantsList = m_mucRoom->participants();
        qDebug() << "participantsList: " << participantsList;
        for( int L = 0; L < participantsList.length(); L++ )
        {
            QString replaceStr = m_jidRoom + "/";
            QString participant = participantsList.at(L);
            participant.replace( replaceStr, QString("") );
            retList.append(participant);
        }
        //qDebug() << "m_mucRoom->participants():" << retList;
        //return m_mucRoom->participants();
    }
    return retList;
}


void XmppMuc::participantChangedAction(QString jid)
{
    QString nickName = resourceAsNickName( jid );
    if( !nickName.isEmpty() ) {
        emit participantChanged( nickName );
    }
}

void XmppMuc::participantAddedAction(QString jid)
{
    QString nickName = resourceAsNickName( jid );
    if( !nickName.isEmpty() ) {
        emit participantAdded( nickName );
    }
}

void XmppMuc::participantRemovedAction(QString jid)
{
    QString nickName = resourceAsNickName( jid );
    if( !nickName.isEmpty() ) {
        emit participantRemoved( nickName );
    }
}

void XmppMuc::messageReceivedAction(const QXmppMessage &message)
{
    //qDebug() << Q_FUNC_INFO;
    //qDebug() << "# from:" << message.from();
    //qDebug() << "# to:" << message.to();
    //qDebug() << "# body:" << message.body();
    //qDebug() << "# thread:" << message.thread();

    QString nickName = resourceAsNickName( message.from() );
    emit messageReceived( nickName, message.body() );

}

void XmppMuc::subjectRoomChangedAction(const QString &subject)
{
    if ( subject != m_subjectRoom )
    {
        m_subjectRoom = subject;
        emit subjectRoomChanged( m_subjectRoom );
    }
}



